package handler

import (
	"gin/global"
	pb "gin/internal/pb/order"
	"gin/internal/request"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func OrderCreate(c *gin.Context) {

	var req request.OrderForm
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "参数获取失败"})
		return
	}
	create, err := global.OrderClient.OrderCreate(c, &pb.OrderCreateRequest{
		UserId: int32(req.UserID),
		Num:    req.Num,
		Notes:  req.Notes,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "订单创建失败s"})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{"message": "订单创建成功", "data": create})
}
func OrderList(c *gin.Context) {
	list, err := global.OrderClient.OrderList(c, &pb.OrderListRequest{})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "订单列表展示失败"})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{"message": "订单列表展示成功", "data": list})
}

func CouponList(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("types"))
	list, err := global.OrderClient.CouponList(c, &pb.CouponListRequest{
		Types: int32(id),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "优惠券列表展示失败"})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{"message": "优惠券列表展示成功", "data": list})
}

func UserCoupon(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("status"))
	list, err := global.OrderClient.UserCoupon(c, &pb.UserCouponRequest{
		Status: int32(id),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "我的优惠券列表展示失败"})
		return
	}
	c.JSON(http.StatusBadRequest, gin.H{"message": "我的优惠券列表展示成功", "data": list})
}
